package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.TeamFilesReview;
import cn.cdeden.knowledge.domain.dto.TeamFilesReviewDto;
import cn.cdeden.knowledge.domain.dto.TeamFilesReviewQueryParam;
import cn.cdeden.knowledge.mapper.TeamFilesReviewMapper;
import cn.cdeden.knowledge.service.TeamFilesReviewService;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Chen
* @date 2023-11-23
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = TeamFilesReviewService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamFilesReviewServiceImpl extends ServiceImpl<TeamFilesReviewMapper, TeamFilesReview> implements TeamFilesReviewService {

    // private final RedisUtils redisUtils;
    private final TeamFilesReviewMapper teamFilesReviewMapper;

    @Override
    public TableDataInfo<TeamFilesReviewDto> queryAll(TeamFilesReviewQueryParam query, Pageable pageable) {
        IPage<TeamFilesReview> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<TeamFilesReview> page = teamFilesReviewMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        TableDataInfo<TeamFilesReviewDto> teamFilesReviewDtoTableDataInfo = MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), TeamFilesReviewDto.class);
//        PageInfo<TeamFilesReviewDto> teamFilesReviewDtoPageInfo = ConvertUtil.convertPage(page, TeamFilesReviewDto.class, pageable);
        List<TeamFilesReviewDto> content = teamFilesReviewDtoTableDataInfo.getRows();
        for (TeamFilesReviewDto teamFilesReviewDto : content) {
            teamFilesReviewDto.setReplierName(teamFilesReviewDto.getReplier());
            teamFilesReviewDto.setNickName(teamFilesReviewDto.getUserId());
        }
        return teamFilesReviewDtoTableDataInfo;
    }

    @Override
    public List<TeamFilesReviewDto> queryAll(TeamFilesReviewQueryParam query){
        return MapstructUtils.convert(teamFilesReviewMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), TeamFilesReviewDto.class);
    }

    @Override
    public TeamFilesReview getById(Long id) {
        return teamFilesReviewMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public TeamFilesReviewDto findById(Long id) {
        return MapstructUtils.convert(getById(id), TeamFilesReviewDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(TeamFilesReviewDto resources) {
        TeamFilesReview entity = MapstructUtils.convert(resources, TeamFilesReview.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(TeamFilesReviewDto resources){
        TeamFilesReview entity = MapstructUtils.convert(resources, TeamFilesReview.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Long> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<TeamFilesReviewDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (TeamFilesReviewDto teamFilesReview : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("用户编号", teamFilesReview.getUserId());
              map.put("创建时间", teamFilesReview.getCreateTime());
              map.put("树节点id", teamFilesReview.getTreeNodeId());
              map.put("回复评论id", teamFilesReview.getRecoverReviewId());
              map.put("内容", teamFilesReview.getContent());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
